<template>
  <view>
    <!-- 收银台 -->
    <view class="edgeInsetTop"></view>
    <view class="wanl-pay">
      <view class="price-box">
        <text class="text-df">{{ order_no }}</text>
        <text class="wanl-pip text-price margin-top-sm">{{ price }}</text>
        <block v-if="score * 1">
          <f-money
            :money="score * 1"
            :unit="integralName"
            :size="60"
            color="#333333"
            :unitSize="40"
            unitPosition="right"
            :showZeroDecimal="false"
          />
        </block>
      </view>
      <view class="list-box">
        <view class="item" v-for="(item, index) in payList" :key="index">
          <text :class="'wlIcon-' + item.type + '-pay'"></text>
          <view class="action">
            <text class="title wanl-pip">{{ item.name }}</text>
            <view v-if="item.type == 'balance'">
              <text v-if="isbalance">可用余额 ￥{{ user.money }}</text>
              <text v-else>余额不足，可用余额 ￥{{ user.money }}</text>
            </view>
            <view v-else>{{ item.describe }}</view>
          </view>
          <view class="radio text-xxl" v-if="item.state" @tap="onSelect(index)">
            <text v-if="item.select" class="wlIcon-xuanze-danxuan wanl-orange"></text>
            <text v-else class="wlIcon-xuanze wanl-gray-light"></text>
          </view>
          <view class="radio text-xxl" v-else>
            <text class="wlIcon-xuanze-danxuan wanl-gray-light"></text>
          </view>
        </view>
      </view>
      <view v-if="disabled">
        <button class="mix-btn wanl-bg-redorange">
          <text class="wlIcon-jiazai wlIconfont-spin margin-right-xs"></text>正在支付中...
        </button>
      </view>
      <view v-else>
        <button v-if="payNum == 1" class="mix-btn wanl-bg-redorange" @tap="confirm()">确认支付</button>
        <button v-else class="mix-btn wanl-bg-redorange" @tap="confirm()">合并支付</button>
      </view>
      <view class="footer text-center">
        <view class="text-sm" v-if="order_pay_no"
          >{{ order_type === 'groups' ? '拼团' : '' }}交易号：{{ order_pay_no }}
        </view>
        <view> © 图奕支付</view>
      </view>
    </view>
  </view>
</template>
<script>
import { mapState, mapGetters } from 'vuex';
export default {
  computed: {
    ...mapGetters('common', ['integralName']),
    ...mapState(['user']),
  },
  data() {
    return {
      token: '',
      disabled: false,
      price: '0.00',
      score: '0',
      isbalance: false,
      order_type: 'goods',
      order_no: '',
      order_pay_no: '',
      payNum: 1, // 支付方式 独立支付-合并支付
      payList: [],
      order_sn: '', //用于vip
      apply: 0, //是否是从商家入驻来的。0--不是，1--是
      orderId: null, //商家入驻的订单id
    };
  },

  watch: {
    price(val, newval) {
      if (val <= parseFloat(this.$store.state.user.money)) {
        this.isbalance = true;
        // this.getPayment();
        this.payList.forEach((item, index) => {
          if (item.type == 'balance') {
            item.state = true;
            this.$set(this.payList, index, item);
            return;
          }
        });
      }
    },
  },

  onBackPress(e) {
    if (this.order_sn == '') {
      uni.switchTab({
        url: '/pages/user',
      });
      return true; //表示禁止默认返回
    }
  },
  onLoad(option) {
    console.log('onLoad(option) {========>', option);
    if (!option.apply) {
      this.$api.post({
        url: '/wanlshop/pay/getBalance',
        success: res => {
          this.$store.commit('user/setUserInfo', {
            money: res,
          });
        },
      });
    } else {
      this.apply = 1;
    }
    if (option.data) {
      var sum = 0,
        data = JSON.parse(option.data),
        order_ids = [];
      if (data.length <= 1) {
        this.price = data[0].price;
        this.score = data[0].score;
        this.order_type = data[0].type;
        this.order_no = `${data[0].type == 'groups' ? '拼团' : ''}订单号：${data[0].order_no}`;
        this.order_pay_no = data[0].pay_no;
        this.order_id = data[0].order_id;
      } else {
        data.forEach(item => {
          sum = this.$wanlshop.bcadd(sum, item.price);
          order_ids.push(item.order_id);
        });
        this.price = sum;
        this.order_id = order_ids;
        this.payNum = data.length;
        this.order_no = '合并支付 ' + data.length + '个订单';
      }
      this.token = option.token;
    } else if (option.order_id && !option.apply) {
      uni.showLoading({
        title: '结算中...',
      });
      this.$api.post({
        url: '/wanlshop/pay/getPay',
        data: {
          order_id: option.order_id,
          order_type: option.order_type,
        },
        success: res => {
          uni.hideLoading();
          this.token = res.token;
          this.price = res.price;
          this.order_type = res.order_type;
          this.order_no = `${res.order_type == 'groups' ? '拼团' : ''}订单号：${res.order_no}`;
          this.order_pay_no = res.pay_no;
          this.order_id = res.order_id;
        },
      });
    } else if (option.vipPay !== undefined) {
      const data = JSON.parse(option.vipPay);
      this.price = data.money;
      this.order_sn = data.order_sn;
    } else if (option.apply && option.order_id) {
      // 商家入驻支付
      this.orderId = option.order_id;
      this.price = option.money;
    } else {
      console.log('非法访问');
    }
    // 获取支付列表
    this.getPayment();
  },
  methods: {
    getPayment() {
      let method = 'wap';

      // if ( this.order_sn || this.orderId ) {
      // 	method = 'mp';
      // }
      // #ifdef H5
      if (this.$jssdk.isWechat()) {
        method = 'mp';
      }
      // #endif

      // #ifdef APP-PLUS
      method = 'app';
      // #endif
      // // #ifdef MP-BAIDU
      // method = 'mini';
      // // #endif
      // #ifdef MP-WEIXIN
      method = 'miniapp';
      // #endif
      // // #ifdef MP-ALIPAY
      // method = 'mini';
      // // #endif
      // // #ifdef MP-QQ
      // method = 'mini';
      // // #endif
      if (this.price <= parseFloat(this.$store.state.user.money)) {
        this.isbalance = true;
        // this.getPayment();
        this.payList.forEach((item, index) => {
          if (item.type == 'balance') {
            item.state = true;
            this.$set(this.payList, index, item);
            return;
          }
        });
      }
      this.payList = [
        {
          name: '余额支付',
          describe: '',
          type: 'balance',
          method: 'balance',
          state: this.isbalance, // 是否可用
          select: this.isbalance, // 是否选中
        },
      ];
      // #ifdef H5
      if (this.$jssdk.isWechat()) {
        // 基本完成,后端未测试,没有完善回调，1.0.8 版本暂时屏蔽
        this.payList.push({
          name: '微信支付',
          describe: '推荐使用微信支付',
          type: 'wechat', // jssdk
          method: method,
          state: true,
          select: !this.isbalance,
        });
      } else {
        this.payList.push(
          {
            name: '支付宝',
            describe: '',
            type: 'alipay',
            method: method,
            state: true,
            select: false,
          },
          {
            name: '微信支付',
            describe: '推荐使用微信支付',
            type: 'wechat',
            method: method,
            state: true,
            select: !this.isbalance,
          },
        );
      }
      // #endif
      // #ifndef H5
      uni.getProvider({
        service: 'payment',
        success: e => {
          e.provider.map(value => {
            if (value == 'alipay') {
              this.payList.push({
                name: '支付宝',
                describe: '',
                type: value,
                method: method,
                state: true,
                select: false,
              });
            } else if (value == 'wxpay') {
              this.payList.push({
                name: '微信支付',
                describe: '推荐使用微信支付',
                type: 'wechat',
                method: method,
                state: true,
                select: !this.isbalance,
              });
            }
            //  else if ( value == 'baidu' ) {
            // 	this.payList.push( {
            // 		name: '百度收银台',
            // 		describe: '',
            // 		type: value,
            // 		method: method,
            // 		state: true,
            // 		select: false
            // 	} );
            // } else if ( value == 'appleiap' ) {
            // 	this.payList.push( {
            // 		name: 'Apple支付',
            // 		describe: '',
            // 		type: 'apple',
            // 		method: method,
            // 		state: true,
            // 		select: false
            // 	} );
            // }
          });
        },
      });
      // #endif
      // console.log('支付 ========>',this.payList);

      let map = new Map();
      for (let item of this.payList) {
        if (!map.has(item.type)) {
          map.set(item.type, item);
        }
      }
      this.$set(this, 'payList', [...map.values()]);
    },
    //确认支付
    confirm() {
      let data = null;
      if (this.disabled) {
        return;
      }
      this.payList.map((value, index, array) => {
        if (value.select) {
          data = value;
        } else {
        }
      });
      // 判断支付是否存在
      if (!data) {
        uni.showModal({
          content: '请选择支付方式',
          showCancel: false,
        });
      } else {
        this.disabled = true;

        if (data.type == 'balance') {
          uni.showModal({
            content: '是否确认余额支付？',
            success: response => {
              if (response.confirm) {
                this.wanlPay(data);
              }
            },
          });
        } else {
          // 获取小程序code
          // #ifdef MP
          uni.login({
            success: e => {
              this.wanlPay(data, e.code);
            },
            fail: e => {
              uni.showModal({
                content: '无法获取微信code,原因为: ' + e.errMsg,
                showCancel: false,
              });
            },
          });
          // #endif
          // #ifndef MP
          this.wanlPay(data);
          // #endif
        }
      }
    },
    async wanlPay(data, code = null) {
      const that = this;
      let url = '';
      let sendData = {};
      if (this.apply == 1) {
        // 商家入驻支付接口
        url = '/wanlshop/pay/shopPay';
        sendData = {
          type: data.type,
          method: data.method,
          code: code,
          order_id: this.orderId,
        };
      } else {
        // 订单支付接口
        url = '/distribution/user_upgrade/pay';
        sendData = {
          type: data.type,
          method: data.method,
          code: code,
          order_sn: this.order_sn,
          // order_type: this.order_type,
          // order_id: this.order_id,
          // token: this.token,
        };
      }
      if (this.order_sn || this.orderId) {
        this.$api.post({
          url,
          data: sendData,
          baseUrl: this.apply == 1 ? this.$wanlshop.config('appurl') : this.$wanlshop.config('vipBaseUrl'),
          success: res => {
            // 余额支付/小程序支付
            if (data.type == 'balance') {
              this.$store.commit('user/setUserInfo', {
                money: this.$wanlshop.bcsub(this.$store.state.user.money, this.price),
              });
              this.paySuccess();
            }
            // 微信 公支付
            if (data.type == 'wechat' && data.method == 'mp') {
              // 1.0.8升级 延迟弹出

              console.log('  微信支付  res ========>', res);
              // wx.chooseWXPay({
              // 	// appId: res.data.appId,
              // 	timestamp: res.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
              // 	nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
              // 	package: res.packageValue, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
              // 	signType: res.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
              // 	paySign: res.paySign, // 支付签名
              // 	success: function(res) {
              // 		// 支付成功后的回调函数
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: function(res) {
              // 		console.log('支付失败');
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	}
              // });
              const onBridgeReady = () => {
                WeixinJSBridge.invoke(
                  'getBrandWCPayRequest',
                  {
                    appId: res.appId,
                    timeStamp: res.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                    nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
                    package: res.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                    signType: res.signType, // 微信支付V3的传入RSA,微信支付V2的传入格式与V2统一下单的签名格式保持一致
                    paySign: res.paySign, // 支付签名
                  },
                  function (res) {
                    console.log(res);
                    // alert(JSON.stringify("支付消息：\n" + JSON.stringify(res)));
                    // alert("支付消息：\n"+res.err_msg);
                    // that.$wanlshop.msg( '支付' );
                    // alert('this: ' + !that)
                    if (res.err_msg == 'get_brand_wcpay_request:ok') {
                      that.$wanlshop.msg('支付成功');
                      // 使用以上方式判断前端返回,微信团队郑重提示：
                      //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                      // 支付成功后的回调函数
                      that.paySuccess();
                      that.disabled = false;
                    } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                      that.$wanlshop.msg('取消支付');
                      that.disabled = false;
                    } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                      that.$wanlshop.msg('支付失败');
                      that.disabled = false;
                    } else {
                      if (err.errMsg == 'chooseWXPay:ok') {
                        that.$wanlshop.msg('支付成功');
                      } else if (err.errMsg == 'chooseWXPay:cancel') {
                        that.$wanlshop.msg('支付失败，取消支付');
                      } else if (err.errMsg == 'chooseWXPay:fail') {
                        that.$wanlshop.msg('支付失败');
                      }
                      that.disabled = false;
                    }
                  },
                );
              };
              if (typeof WeixinJSBridge == 'undefined') {
                if (document.addEventListener) {
                  document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                  document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                  document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
              } else {
                onBridgeReady();
              }

              // this.$jssdk.wxpay( {
              // 	data: res,
              // 	success: ()  => {
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: err => {
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	},
              // } )
            }
            // 支付宝 H5支付
            if (data.type == 'alipay' && data.method == 'wap') {
              this.disabled = false;
              if (this.order_type === 'goods') {
                this.$store.commit('statistics/order', {
                  pay: this.$wanlshop.bcsub(this.$store.state.statistics.order.pay, this.payNum),
                  delive: this.$wanlshop.bcadd(this.$store.state.statistics.order.delive, this.payNum),
                });
              }
              document.write(res);
            }
            // 微信小程序支付
            if (data.type == 'wechat' && data.method == 'miniapp') {
              uni.requestPayment({
                appId: res.appId,
                nonceStr: res.nonceStr,
                package: res.package,
                paySign: res.paySign,
                signType: res.signType,
                timeStamp: res.timeStamp,
                success: e => {
                  this.paySuccess();
                  this.disabled = false;
                },
                fail: e => {
                  uni.showModal({
                    content: '支付失败,原因为: ' + e.errMsg,
                    showCancel: false,
                  });
                  this.disabled = false;
                },
              });
            }
            // 微信h5支付，返回的是链接
            if (data.type == 'wechat' && data.method == 'wap') {
              // this.$jssdk.wxpay( {
              // 	data: res,
              // 	success: res => {
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: err => {
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	}
              // } )
              setTimeout(() => {
                uni.showModal({
                  content: '请确认微信支付是否已完成',
                  confirmText: '已完成',
                  cancelText: '重新支付',
                  success: response => {
                    if (response.confirm) {
                      this.paySuccess();
                    } else if (response.cancel) {
                      this.confirm();
                    }
                  },
                });
              }, 2000);
              // 异步查询是否支付成功
              window.location.href = res;
            }
            // APP支付
            if ((data.type == 'alipay' || data.type == 'wechat') && data.method == 'app') {
              let provider = data.type;
              if (data.type == 'wechat') {
                provider = 'wxpay';
              }
              uni.requestPayment({
                provider: provider,
                orderInfo: res,
                success: e => {
                  console.log('success', e);
                  this.paySuccess();
                },
                fail: e => {
                  uni.showModal({
                    content: '支付失败,原因为: ' + e.errMsg,
                    showCancel: false,
                  });
                },
                complete: () => {
                  this.disabled = false;
                },
              });
            }
          },
          fail: e => {
            this.disabled = false;
          },
        });
      } else {
        this.$api.post({
          url: '/wanlshop/pay/payment',
          data: {
            order_type: this.order_type,
            type: data.type,
            method: data.method,
            code: code,
            order_id: this.order_id,
            token: this.token,
          },
          success: res => {
            // 余额支付/小程序支付
            if (data.type == 'balance') {
              this.$store.commit('user/setUserInfo', {
                money: this.$wanlshop.bcsub(this.$store.state.user.money, this.price),
              });
              this.paySuccess();
            }
            // 微信 公支付
            if (data.type == 'wechat' && data.method == 'mp') {
              // 1.0.8升级 延迟弹出

              console.log('  微信支付  res ========>', res);
              // wx.chooseWXPay({
              // 	// appId: res.data.appId,
              // 	timestamp: res.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
              // 	nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
              // 	package: res.packageValue, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
              // 	signType: res.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
              // 	paySign: res.paySign, // 支付签名
              // 	success: function(res) {
              // 		// 支付成功后的回调函数
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: function(res) {
              // 		console.log('支付失败');
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	}
              // });
              const onBridgeReady = () => {
                WeixinJSBridge.invoke(
                  'getBrandWCPayRequest',
                  {
                    appId: res.appId,
                    timeStamp: res.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                    nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
                    package: res.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                    signType: res.signType, // 微信支付V3的传入RSA,微信支付V2的传入格式与V2统一下单的签名格式保持一致
                    paySign: res.paySign, // 支付签名
                  },
                  function (res) {
                    console.log(res);
                    // alert(JSON.stringify("支付消息：\n" + JSON.stringify(res)));
                    // alert("支付消息：\n"+res.err_msg);
                    // that.$wanlshop.msg( '支付' );
                    // alert('this: ' + !that)
                    if (res.err_msg == 'get_brand_wcpay_request:ok') {
                      that.$wanlshop.msg('支付成功');
                      // 使用以上方式判断前端返回,微信团队郑重提示：
                      //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                      // 支付成功后的回调函数
                      that.paySuccess();
                      that.disabled = false;
                    } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                      that.$wanlshop.msg('取消支付');
                      that.disabled = false;
                    } else if (res.err_msg == 'get_brand_wcpay_request:fail') {
                      that.$wanlshop.msg('支付失败');
                      that.disabled = false;
                    } else {
                      if (err.errMsg == 'chooseWXPay:ok') {
                        that.$wanlshop.msg('支付成功');
                      } else if (err.errMsg == 'chooseWXPay:cancel') {
                        that.$wanlshop.msg('支付失败，取消支付');
                      } else if (err.errMsg == 'chooseWXPay:fail') {
                        that.$wanlshop.msg('支付失败');
                      }
                      that.disabled = false;
                    }
                  },
                );
              };
              if (typeof WeixinJSBridge == 'undefined') {
                if (document.addEventListener) {
                  document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                  document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                  document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
              } else {
                onBridgeReady();
              }

              // this.$jssdk.wxpay( {
              // 	data: res,
              // 	success: ()  => {
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: err => {
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	},
              // } )
            }
            // 支付宝 H5支付
            if (data.type == 'alipay' && data.method == 'wap') {
              this.disabled = false;
              if (this.order_type === 'goods') {
                this.$store.commit('statistics/order', {
                  pay: this.$wanlshop.bcsub(this.$store.state.statistics.order.pay, this.payNum),
                  delive: this.$wanlshop.bcadd(this.$store.state.statistics.order.delive, this.payNum),
                });
              }
              document.write(res);
            }
            // 微信小程序支付
            if (data.type == 'wechat' && data.method == 'miniapp') {
              uni.requestPayment({
                appId: res.appId,
                nonceStr: res.nonceStr,
                package: res.package,
                paySign: res.paySign,
                signType: res.signType,
                timeStamp: res.timeStamp,
                success: e => {
                  this.paySuccess();
                  this.disabled = false;
                },
                fail: e => {
                  uni.showModal({
                    content: '支付失败,原因为: ' + e.errMsg,
                    showCancel: false,
                  });
                  this.disabled = false;
                },
              });
            }
            // 微信h5支付，返回的是链接
            if (data.type == 'wechat' && data.method == 'wap') {
              // this.$jssdk.wxpay( {
              // 	data: res,
              // 	success: res => {
              // 		this.paySuccess();
              // 		this.disabled = false;
              // 	},
              // 	fail: err => {
              // 		if ( err.errMsg == "chooseWXPay:ok" ) {
              // 			this.$wanlshop.msg( '支付成功' );
              // 		} else if ( err.errMsg == "chooseWXPay:cancel" ) {
              // 			this.$wanlshop.msg( '支付失败，取消支付' );
              // 		} else if ( err.errMsg == "chooseWXPay:fail" ) {
              // 			this.$wanlshop.msg( '支付失败' );
              // 		}
              // 		this.disabled = false;
              // 	}
              // } )
              setTimeout(() => {
                uni.showModal({
                  content: '请确认微信支付是否已完成',
                  confirmText: '已完成',
                  cancelText: '重新支付',
                  success: response => {
                    if (response.confirm) {
                      this.paySuccess();
                    } else if (response.cancel) {
                      this.confirm();
                    }
                  },
                });
              }, 2000);
              // 异步查询是否支付成功
              window.location.href = res;
            }
            // APP支付
            if ((data.type == 'alipay' || data.type == 'wechat') && data.method == 'app') {
              let provider = data.type;
              if (data.type == 'wechat') {
                provider = 'wxpay';
              }
              uni.requestPayment({
                provider: provider,
                orderInfo: res,
                success: e => {
                  console.log('success', e);
                  this.paySuccess();
                },
                fail: e => {
                  uni.showModal({
                    content: '支付失败,原因为: ' + e.errMsg,
                    showCancel: false,
                  });
                },
                complete: () => {
                  this.disabled = false;
                },
              });
            }
          },
        });
      }
    },
    onSelect(key) {
      this.payList.map((value, index, array) => {
        if (index == key) {
          value.select = !value.select;
        } else {
          value.select = false;
        }
      });
    },
    // 支付成功
    paySuccess() {
      this.disabled = false;
      if (this.order_type === 'goods') {
        this.$store.commit('statistics/order', {
          pay: this.$wanlshop.bcsub(this.$store.state.statistics.order.pay, this.payNum),
          delive: this.$wanlshop.bcadd(this.$store.state.statistics.order.delive, this.payNum),
        });
      }
      if (this.order_sn !== '') {
        this.$wanlshop.to(`/pages/page/success?type=pay&order_type=vip`);
      } else {
        this.$wanlshop.to(`/pages/page/success?type=pay&order_type=${this.order_type ? this.order_type : 'goods'}`);
      } // TODO
      if (this.apply == 1) {
        this.$wanlshop.to(`/pages/page/success?type=pay`);
      }
    },
  },
};
</script>
<style>
page {
  background-color: white;
}

radio-group {
  display: block;
}
</style>
